﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Xin478.CSWork.Client
{
    public class MyHoverImg :System.Windows.Forms.PictureBox
    {
        public Image HoverImage { get; set; }
        public Image ClickingImage { get; set; }
        public Image ClickedImage { get; set; }
        public Image HoverBackgroundImage { get; set; }
        public Image ClickingBackgroundImage { get; set; }
        public Image ClickedBackgroundImage { get; set; }
        public bool LockClickStatus { get; set; }
        Image img,bkimg;

        bool ischecked = false;

        public bool Checked
        {
            get
            {
                return ischecked;
            }
            set
            {
                if (ischecked == value) return;

                if (value)
                {
                    if (ClickedBackgroundImage != null) this.BackgroundImage = ClickedBackgroundImage;
                    if (ClickedImage != null) this.Image = ClickedImage;
                }
                else
                {
                    if (ClickedBackgroundImage != null) this.BackgroundImage = bkimg;
                    if (ClickedImage != null) this.Image = img;
                }
                
                ischecked = value;
                if (CheckedChanged != null) CheckedChanged(this, null);
            }
        }


        public event EventHandler CheckedChanged;

        protected override void OnMouseHover(EventArgs e)
        {
            if (!ischecked)
            {
                if (img == null)
                {
                    img = this.Image;
                    bkimg = this.BackgroundImage;
                }
                if (HoverImage != null) this.Image = HoverImage;
                if (HoverBackgroundImage != null) this.Image = HoverBackgroundImage;
            }
            base.OnMouseHover(e);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (!ischecked)
            {
                if (ClickingImage != null) this.Image = ClickingImage;
                if (ClickingBackgroundImage != null) this.Image = ClickingBackgroundImage;
                this.Checked = true;               
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (!LockClickStatus && this.ischecked) this.Checked = false;     
            if (!ischecked)
            {
                if (ClickedImage != null) this.Image = ClickedImage;
                if (ClickedBackgroundImage != null) this.Image = ClickedBackgroundImage;
            }
            base.OnMouseUp(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            if (!ischecked)
            {
                this.Image = img;
                this.BackgroundImage = bkimg;
            }
            base.OnMouseLeave(e);
        }
    }
}
